//
//  CreateJobReviewConnectTypeView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/6/13.
//

import UIKit
import SnapKit
class CreateJobReviewConnectTypeView: View {
    
    let connectedTypeText = CreateJobReviewContentLabel()
    let gridContentView = View()
    let nmiText = CreateJobReviewContentLabel()
    let gridConnectionApplicationRefNOText = CreateJobReviewContentLabel()
    
    override func setUpView() {
        //connect type
        let connectTypeTitle = CreateJobReviewTitleLabel(R.string.localizable.connectType())
        self.addSubview(connectTypeTitle)
        connectTypeTitle.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        //
        connectedTypeText.numberOfLines = 0
        connectedTypeText.textAlignment = .right
        self.addSubview(connectedTypeText)
        connectedTypeText.snp.makeConstraints { make in
            make.centerY.equalTo(connectTypeTitle)
            make.right.equalToSuperview()
            make.size.greaterThanOrEqualTo(0)
        }
        //content view
        self.addSubview(gridContentView)
        gridContentView.snp.makeConstraints { make in
            make.top.equalTo(connectedTypeText.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        
    }
    func setData(){
        self.connectedTypeText.text = CreateJobModelHelper.shared.createJobViewModel.systemDetail.connected_type_name
        self.setGridConnectView()
    }
    //根据数据显示connect type的nmi和gridConnectionApplicationRefNo
    private func setGridConnectView(){
        //先清除
        for subView in self.gridContentView.subviews{
            subView.removeFromSuperview()
        }
        if CreateJobModelHelper.shared.createJobViewModel.systemDetail.connected_type == .onGrid {
            //nmi
            let nmiTitle = CreateJobReviewTitleLabel(R.string.localizable.nationalMeterIdentifier())
            nmiTitle.numberOfLines = 2
            gridContentView.addSubview(nmiTitle)
            nmiTitle.snp.makeConstraints { make in
                make.top.equalTo(12)
                make.left.equalToSuperview()
                make.size.greaterThanOrEqualTo(0)
            }
            let nmiText = CreateJobReviewContentLabel()
            nmiText.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
            nmiText.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
            nmiText.textAlignment = .right
            nmiText.text = CreateJobModelHelper.shared.createJobViewModel.systemDetail.nmi
            gridContentView.addSubview(nmiText)
            nmiText.snp.makeConstraints { make in
                make.top.equalTo(nmiTitle)
                make.left.equalTo(nmiTitle.snp.right).offset(5)
                make.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(nmiTitle)
                if CreateJobModelHelper.shared.createJobViewModel.systemDetail.gridConnectionApplicationRefNo == "" {//为空不显示ref no
                    make.bottom.equalToSuperview()
                }
            }
            //
            if CreateJobModelHelper.shared.createJobViewModel.systemDetail.gridConnectionApplicationRefNo != "" {
                let refNoTitle = CreateJobReviewTitleLabel(R.string.localizable.gridConnectionApplicationRefNo())
                refNoTitle.numberOfLines = 2
                refNoTitle.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
                refNoTitle.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
                gridContentView.addSubview(refNoTitle)
                refNoTitle.snp.makeConstraints { make in
                    make.top.equalTo(nmiTitle.snp.bottom).offset(12)
                    make.left.equalToSuperview()
                    make.size.greaterThanOrEqualTo(0)
                }
                //
                let gridConnectionApplicationRefNoText = CreateJobReviewContentLabel()
                gridConnectionApplicationRefNoText.numberOfLines = 0
                gridConnectionApplicationRefNoText.textAlignment = .right
                gridConnectionApplicationRefNoText.text = CreateJobModelHelper.shared.createJobViewModel.systemDetail.gridConnectionApplicationRefNo
                gridContentView.addSubview(gridConnectionApplicationRefNoText)
                gridConnectionApplicationRefNoText.snp.makeConstraints { make in
                    make.top.equalTo(refNoTitle)
                    make.left.equalTo(refNoTitle.snp.right).offset(5)
                    make.right.equalToSuperview()
                    make.height.greaterThanOrEqualTo(0)
                    make.bottom.equalToSuperview()
                }
            }
        }else{
            
        }
    }
}
